//
//  ZCTextField.m
//  BaiSi
//
//  Created by 张灿 on 16/4/29.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCTextField.h"

@implementation ZCTextField

- (void)awakeFromNib
{
    //设置光标颜色
    self.tintColor = [UIColor whiteColor];
    //监听文本框编辑
    [self addTarget:self action:@selector(textBegin) forControlEvents:UIControlEventEditingDidBegin];
    
    [self addTarget:self action:@selector(textEnd) forControlEvents:UIControlEventEditingDidEnd];
    
    [self setPlaceholderColor:[UIColor lightGrayColor]];
}

//开始编辑
- (void)textBegin
{
    [self setPlaceholderColor:[UIColor whiteColor]];
}

//结束编辑,恢复颜色
- (void)textEnd
{
    [self setPlaceholderColor:[UIColor lightGrayColor]];
}

//封装设置颜色的方法
- (void)setPlaceholderColor:(UIColor *)color
{
    //创建字典保存字体属性
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    //设置颜色
    dict[NSForegroundColorAttributeName] = color;
    //给占位字符设置
    NSAttributedString *attr = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
    self.attributedPlaceholder = attr;
}
@end
